package Backtracking;
/*
 * 组合总和 III
 */
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class lc216 {

    List<List<Integer>> result = new ArrayList<>();
    LinkedList<Integer> path = new LinkedList<>();
    public List<List<Integer>> combinationSum3(int k, int n) {
        barktracking(k, n, 1, 0);
        return result;
    }
    public void barktracking(int k, int n, int startIndex, int sum) {
        if (sum > n) {
            return;
        }
        if (path.size() == k) {
            if (sum == n) {
                result.add(new ArrayList<>(path));
            }
            return;
        }
        for (int i = startIndex; i <= 9; i++) {
            path.add(i);
            sum += i;
            barktracking(k, n, i + 1, sum);
            path.removeLast();
            sum -= i;
        }
    }

}
